from Case.python_ui.Common.basepage import Basepage
from selenium.common.exceptions import NoSuchElementException, ElementClickInterceptedException
import time, pytest
from Case.python_ui.PageLocate.user_my_coupon_locator import CouponMyCouponLocator
from Case.python_ui.Pageobject.admin_coupon_template_page import CouponTemplatePage
from Case.python_ui.PageLocate.admin_coupon_coupon_template_locator import CouponCouponTemplateLocator
from Case.python_ui.PageLocate.admin_coupon_coupon_records_locator import AdminCouponCouponRecordsLocator

class MyCouponPage(Basepage):

    #判断是否包含元素
    def check_contain_str(self, string, except_string):
        if except_string in string:
            assert True
        else:
            assert False , '实际值:' + string + '期望值:' + except_string

    # 校验某个属性值是否符合预期
    def check_attribute(self, real_result, except_result):
        if real_result == except_result:
            assert True
        else:
            assert False, '实际值:' + real_result + '期望值:' + except_result

    # 元素是否存在
    def element_is_exist(self, locate):
        doc = '判断元素是否存在'
        try:
            self.get_element(locate, doc)
        except NoSuchElementException as e:
            return False
        else:
            return True

    # 判断按钮是否可点击，可以则点击，不可以就什么都不操作
    def clear_filter_status(self, locate):
        doc = '判断按钮是否可点击'
        try:
            #点击筛选按钮
            self.click_element(locate, doc)
            time.sleep(2)
            #点击reset按钮
            self.click_element(CouponMyCouponLocator.status_filter_reset_button, doc)
        except ElementClickInterceptedException as e:
            self.click_element(CouponMyCouponLocator.status_filter_ok_button, doc)
            return
        else:
            self.click_element(CouponMyCouponLocator.status_filter_ok_button, doc)
            return

    # # 清空筛选条件
    # def clear_filter_status(self, locate):
    #     doc = '清空筛选条件'
    #     self.click_element(locate, doc)
    #     time.sleep(2)
    #     self.button_is_enabled(locate)
    #     self.click_element(CouponMyCouponLocator.status_filter_reset_button, doc)


    #进入My Coupon页面
    def enter_my_coupon_page(self):
        time.sleep(2)
        doc = "进入My Coupon页面"
        self.wait_elevisble(CouponMyCouponLocator.my_coupon_menu, doc)
        self.click_element(CouponMyCouponLocator.my_coupon_menu, doc)
        time.sleep(3)

    #校验列表元素
    #时区，货币单位需要单独提出来，根据配置文件来校验  --待优化
    def check_list_attribute(self):
        time.sleep(2)
        doc = "校验列表元素"
        my_coupon_list = ['Coupon No.','Status','Coupon Template','Usable Product','Percent','Fixed Amount','Deducted by Coupons', 'Coupon Balance','Reuse','Validation Period']
        my_coupon_list_no = self.get_element(CouponMyCouponLocator.my_coupon_list_no, doc).text
        self.check_contain_str(my_coupon_list_no, my_coupon_list[0])
        my_coupon_list_status = self.get_element(CouponMyCouponLocator.my_coupon_list_status, doc).text
        self.check_contain_str(my_coupon_list_status, my_coupon_list[1])
        my_coupon_list_template = self.get_element(CouponMyCouponLocator.my_coupon_list_template, doc).text
        self.check_contain_str(my_coupon_list_template, my_coupon_list[2])
        my_coupon_list_template = self.get_element(CouponMyCouponLocator.my_coupon_list_template, doc).text
        self.check_contain_str(my_coupon_list_template, my_coupon_list[2])
        my_coupon_list_useable_product = self.get_element(CouponMyCouponLocator.my_coupon_list_useable_product, doc).text
        self.check_contain_str(my_coupon_list_useable_product, my_coupon_list[3])
        my_coupon_list_percent = self.get_element(CouponMyCouponLocator.my_coupon_list_percent, doc).text
        self.check_contain_str(my_coupon_list_percent, my_coupon_list[4])
        my_coupon_list_fixed_amount = self.get_element(CouponMyCouponLocator.my_coupon_list_fixed_amount, doc).text
        self.check_contain_str(my_coupon_list_fixed_amount, my_coupon_list[5])
        my_coupon_list_deducted = self.get_element(CouponMyCouponLocator.my_coupon_list_deducted, doc).text
        self.check_contain_str(my_coupon_list_deducted, my_coupon_list[6])
        my_coupon_list_balance = self.get_element(CouponMyCouponLocator.my_coupon_list_balance, doc).text
        self.check_contain_str(my_coupon_list_balance, my_coupon_list[7])
        my_coupon_list_reuse = self.get_element(CouponMyCouponLocator.my_coupon_list_reuse, doc).text
        self.check_contain_str(my_coupon_list_reuse, my_coupon_list[8])
        my_coupon_list_period = self.get_element(CouponMyCouponLocator.my_coupon_list_period, doc).text
        self.check_contain_str(my_coupon_list_period, my_coupon_list[9])
        time.sleep(3)

    #搜索coupon
    def search_coupon(self):
        time.sleep(2)
        doc = "搜索coupon"
        coupon_template = 'lwm_idr'
        couppon_no = 'CO4CY25DN5779LF'
        search_input = [couppon_no, coupon_template]
        # 清空搜索框
        self.keyboard_event(CouponMyCouponLocator.search_input, doc)
        # couppon_no查询
        self.send_keys(CouponMyCouponLocator.search_input, search_input[0], doc)
        time.sleep(2)
        self.click_element(CouponMyCouponLocator.search_button, doc)
        time.sleep(5)
        # 检查是否有查询结果
        self.check_attribute(self.get_element(CouponMyCouponLocator.first_search_result, doc).text, search_input[0])
        # 清空搜索框
        self.keyboard_event(CouponMyCouponLocator.search_input, doc)
        # coupon_template查询
        self.send_keys(CouponMyCouponLocator.search_input, search_input[1], doc)
        time.sleep(2)
        self.click_element(CouponMyCouponLocator.search_button, doc)
        time.sleep(5)
        # 检查是否有查询结果
        self.check_attribute(self.get_element(CouponMyCouponLocator.first_search_result_coupon_template, doc).text, search_input[1])

    # 筛选 coupon
    def filter_coupon(self):
        time.sleep(2)
        status_arry = ['Valid', 'Exhausted', 'Expired']
        doc = '筛选 valid coupon'
        # 筛选valid的
        # self.click_element(CouponMyCouponLocator.status_filter_button, doc)
        # time.sleep(2)
        # 点击reset按钮
        self.clear_filter_status(CouponMyCouponLocator.status_filter_button)
        time.sleep(2)
        self.click_element(CouponMyCouponLocator.status_filter_valid_button, doc)
        time.sleep(2)
        self.click_element(CouponMyCouponLocator.status_filter_ok_button, doc)
        time.sleep(5)
        # 检查筛选结果, coupon 是否是valid状态
        result = self.get_element(CouponMyCouponLocator.first_search_result_status, doc).text
        self.check_attribute(result, status_arry[0])

        # 筛选Exhausted的
        doc = '筛选 valid coupon'
        # self.click_element(CouponMyCouponLocator.status_filter_button, doc)
        # time.sleep(2)
        # 点击reset按钮
        self.clear_filter_status(CouponMyCouponLocator.status_filter_button)
        time.sleep(2)
        #勾选Exhausted
        self.click_element(CouponMyCouponLocator.status_filter_exhausted_button, doc)
        time.sleep(2)
        self.click_element(CouponMyCouponLocator.status_filter_ok_button, doc)
        time.sleep(5)
        # 检查筛选结果, coupon 是否是Exhausted状态
        result = self.get_element(CouponMyCouponLocator.first_search_result_status, doc).text
        self.check_attribute(result, status_arry[1])

        # 筛选Expired的
        doc = '筛选 Expired coupon'
        # self.click_element(CouponMyCouponLocator.status_filter_button, doc)
        # time.sleep(2)
        # 点击reset按钮
        self.clear_filter_status(CouponMyCouponLocator.status_filter_button)
        time.sleep(2)
        #勾选Expired
        self.click_element(CouponMyCouponLocator.status_filter_expired_button, doc)
        time.sleep(2)
        self.click_element(CouponMyCouponLocator.status_filter_ok_button, doc)
        time.sleep(5)
        # 检查筛选结果, coupon 是否是Expired状态
        result = self.get_element(CouponMyCouponLocator.first_search_result_status, doc).text
        self.check_attribute(result, status_arry[2])

    # bind coupon
    def bind_coupon(self, coupon_no):
        time.sleep(2)
        doc = 'bind coupon'
        self.wait_elevisble(CouponMyCouponLocator.bind_coupon_button, doc)
        self.click_element(CouponMyCouponLocator.bind_coupon_button, doc)
        #输入优惠券
        self.wait_elevisble(CouponMyCouponLocator.bind_coupon_input, doc)
        self.send_keys(CouponMyCouponLocator.bind_coupon_input, coupon_no, doc)
        time.sleep(2)
        # 点击ok按钮
        self.click_element(CouponMyCouponLocator.ok_button, doc)
        time.sleep(2)
        #校验绑定结果
        #清空筛选条件
        self.clear_filter_status(CouponMyCouponLocator.status_filter_button)
        time.sleep(2)
        self.wait_elevisble(CouponMyCouponLocator.search_input, doc)
        self.send_keys(CouponMyCouponLocator.search_input, coupon_no, doc)
        time.sleep(2)
        self.click_element(CouponMyCouponLocator.search_button, doc)
        time.sleep(2)
        result = self.get_element(CouponMyCouponLocator.first_search_result, doc).text
        self.check_attribute(result, coupon_no)


    #到admin端校验绑定结果
    def check_admin_bind_result(self, driver, coupon_no, coupon_template_name):
        time.sleep(2)
        CouponTemplatePage(driver).enter_coupon_template_page()
        time.sleep(2)
        doc = '检查generate history列表的券编码是否是绑定状态'
        self.wait_elevisble(CouponCouponTemplateLocator.search_input, doc)
        self.send_keys(CouponCouponTemplateLocator.search_input, coupon_template_name, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.search_button, doc)
        time.sleep(5)
        assert self.get_element(CouponCouponTemplateLocator.first_search_result, doc).text == coupon_template_name
        #点击详情
        self.click_element(CouponCouponTemplateLocator.first_search_result, doc)
        time.sleep(2)
        self.wait_elevisble(CouponCouponTemplateLocator.generate_history_tab, doc)
        self.click_element(CouponCouponTemplateLocator.generate_history_tab, doc)
        time.sleep(2)
        #筛选已绑定的记录
        # 清空筛选条件
        self.clear_filter_status(CouponCouponTemplateLocator.bind_filter_button)
        #筛选Y
        self.click_element(CouponCouponTemplateLocator.bind_filter_button, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.bind_filter_y_button, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.bind_filter_ok_button, doc)
        #检查绑定状态是否是Y
        self.check_attribute(self.get_element(CouponCouponTemplateLocator.first_history_coupon_template, doc).text, coupon_no)
        self.check_attribute(self.get_element(CouponCouponTemplateLocator.first_history_coupon_template_result_bind, doc).text, 'Y')

        #检查coupon record列表的券编码是否是绑定状态
        doc = '检查coupon record列表的券编码是否是绑定状态'
        #进入coupon record 页面
        self.click_element(AdminCouponCouponRecordsLocator.coupon_records_menu, doc)
        time.sleep(2)
        #coupon no 进行搜索
        self.wait_elevisble(AdminCouponCouponRecordsLocator.search_input, doc)
        self.send_keys(AdminCouponCouponRecordsLocator.search_input, coupon_no, doc)
        time.sleep(2)
        self.click_element(AdminCouponCouponRecordsLocator.search_button, doc)
        time.sleep(5)
        #检查绑定状态是否是Y
        assert self.get_element(AdminCouponCouponRecordsLocator.first_search_result, doc).text == coupon_no
        assert self.get_element(AdminCouponCouponRecordsLocator.first_search_result_bind, doc).text == 'Y'


    def testE(self):
        self.clear_filter_status(CouponMyCouponLocator.status_filter_button)
        print('123333')













